package com.cn.shuangseqiu.activity;

import com.cn.shuangseqiu.R;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.Toast;

public class StSplashActivity extends Activity
{
  private Thread mSplashThread;
  protected Handler mHandler;
  private ProgressBar pBar;
  int mLayout = -1;

  boolean isInit = false;

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);

    this.mHandler = new Handler() {
      public void handleMessage(Message msg) {
        if (msg.what == 0) {
          Toast.makeText(StSplashActivity.this, (String)msg.obj, 
            1).show();

          StSplashActivity.this.finish();
        }
        else if (msg.what == 99999) {
          int v = Integer.parseInt(msg.obj.toString());
          StSplashActivity.this.pBar.setProgress(v);
          if (v == 100)
            StSplashActivity.this.pBar.setVisibility(8);
          else if (v == 0)
            StSplashActivity.this.pBar.setVisibility(0);
        }
      }
    };
  }

  protected void ensureUI()
  {
    if (this.isInit)
      return;
    this.isInit = true;

    if (this.mLayout < 0)
      setContentView(R.layout.situo_splash_activity);
    this.pBar = ((ProgressBar)findViewById(R.id.progressBar1));
  }

  public void setContentView(int layoutResID)
  {
    this.mLayout = layoutResID;
    super.setContentView(layoutResID);
  }

  protected void startThread()
  {
    ensureUI();

    this.mSplashThread = new Thread()
    {
      public void run()
      {
        try
        {
          StSplashActivity.this.showProgress(0);
          StSplashActivity.this.runSplash();

          StSplashActivity.this.showProgress(100);
        }
        catch (Exception ex)
        {
          Message toMain = StSplashActivity.this.mHandler.obtainMessage();
          toMain.what = 0;
          toMain.obj = ex.getMessage();
          StSplashActivity.this.mHandler.sendMessage(toMain);
          return;
        }
      }
    };
    this.mSplashThread.start();
  }

  protected void setLayout(int id) {
    this.mLayout = id;
  }

  protected void runSplash()
    throws Exception
  {
  }

  public void showProgress(int i)
  {
    Message toMain = this.mHandler.obtainMessage();
    toMain.what = 99999;
    toMain.obj = Integer.valueOf(i);
    this.mHandler.sendMessage(toMain);
  }

  public void showMsg(String msg)
  {
    Message toMain = this.mHandler.obtainMessage();
    toMain.what = 0;
    toMain.obj = msg;
    this.mHandler.sendMessage(toMain);
  }
}